package com.frame.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frame.common.core.entity.SysPost;
import com.frame.common.core.security.utils.SecurityUtils;
import com.frame.db.util.Condition;
import com.frame.db.util.Query;
import com.frame.stant.constans.UserConstants;
import com.frame.stant.utils.R;
import com.frame.system.service.ISysPostService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "岗位")
@RestController
@RequestMapping("/system/post")
@AllArgsConstructor
public class SysPostController  {

    private final ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:post:list')")
    @GetMapping("/list")
    public R list(SysPost post, Query query) {
        IPage<SysPost> list = postService.selectPostList(Condition.page(query),post);
        return R.success(list);
    }

//    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@custom.hasAnyAuthority('system:post:export')")
//    @GetMapping("/export")
//    public R export(SysPost post) {
//        List<SysPost> list = postService.selectPostList(post);
//        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
//        return util.exportExcel(list, "岗位数据");
//    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:post:query')")
    @GetMapping(value = "/{postId}")
    public R getInfo(@PathVariable Long postId) {
        return R.success(postService.getById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:post:add')")
//    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return R.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return R.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUsername());
        return R.success(postService.save(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:post:edit')")
//    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return R.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return R.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        return R.success(postService.updateById(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:post:remove')")
//    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R remove(@PathVariable(name = "postIds") List<Long> postIds) {
        return R.success(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public R optionselect() {
        List<SysPost> posts = postService.list();
        return R.success(posts);
    }
}
